<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="车牌号" prop="licenseNum">
        <el-input v-model="queryParams.licenseNum" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="品牌类型" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入品牌类型" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="驾驶员" prop="driverId">
        <el-select filterable v-model="queryParams.driverId" placeholder="请选择驾驶员" clearable @change="changeUser"
          :style="{ width: '100%' }">
          <el-option v-for="dict in driverSelectList" :key="dict.driverId" :label="dict.nickName"
            :value="dict.driverId" />
        </el-select>
      </el-form-item>
      <el-form-item label="购买日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item label="车辆状态" prop="carStatusValue">
        <el-select v-model="queryParams.carStatusValue" placeholder="请选择车辆状态">
          <el-option v-for="dict in dict.type.sys_car_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['vehicle:file:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['vehicle:file:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['vehicle:file:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['vehicle:file:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" border :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" />
      <el-table-column width="150" label="车牌号" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.licenseNum
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="品牌类型" prop="brand" />
      <el-table-column :show-overflow-tooltip="true" label="车辆状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_car_status" :value="scope.row.carStatusValue" />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="车辆类型" prop="nickName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_driver_type" :value="scope.row.driverTypeValue" />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="价格(元)" prop="buyMoney" />
      <el-table-column :show-overflow-tooltip="true" label="发动机号" prop="transmitterNumber" />
      <el-table-column :show-overflow-tooltip="true" label="车架号" prop="frameNo" />
      <el-table-column :show-overflow-tooltip="true" label="驾驶员" prop="nickName" />
      <el-table-column width="200" :show-overflow-tooltip="true" label="注册日期" prop="carInfoBo.yearCheckDate" />

      <el-table-column :show-overflow-tooltip="true" label="年检状态" prop="checkState">

      </el-table-column>
      <el-table-column width="120" :show-overflow-tooltip="true" label="下次年检日期" prop="carInfoBo.nextYearCheckDate" />
      <el-table-column :show-overflow-tooltip="true" width="120" label="下次年检天数" prop="checkDay" />
      <el-table-column :show-overflow-tooltip="true" width="120" label="购买日期" prop="buyDate" />



      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['vehicle:file:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicle:file:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['vehicle:file:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listCar, delCar } from "@/api/car/car.js";
import { selectList as driverSelect } from "@/api/car/driver.js";

export default {
  name: "fileCar",
  dicts: ["sys_car_status", "sys_driver_type"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licenseNum: undefined,
        brand: undefined,
        carStatusValue: undefined,
        driverId: undefined,
      },
      // 日期范围
      dateRange: [],
      driverSelectList: [],
      deptOptions: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.driverSelect();
  },
  methods: {
    driverSelect() {
      driverSelect().then((response) => {
        this.driverSelectList = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.carId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.carId || this.ids;
      this.$router.push("./addFile?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push("./addFile?isInfo=true&id=" + row.carId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.carId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "carId",
        "DELETE",
        "licenseNum"
      );
      this.$modal
        .confirm('是否确认删除车牌号为"' + name + '"的数据项？')
        .then(function () {
          return delCar(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addFile");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCar(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/car/export",
        {
          ...this.queryParams,
        },
        `车辆档案记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
